package config

import (
	"fmt"
	"github.com/spf13/viper"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB

func LoadConfig() {
	viper.SetConfigName("config")
	viper.SetConfigType("yaml")
	viper.AddConfigPath(".")
	if err := viper.ReadInConfig(); err != nil {
		panic(fmt.Errorf("Fatal error config file: %s \n", err))
	}

}

func InitDB() {
	uri := viper.GetString("mysql.uri")
	db, err := gorm.Open(mysql.Open(uri), &gorm.Config{})
	if err != nil {
		panic(fmt.Errorf("Failed to connect database:  %s \n", err))
	}
	DB = db
}
